#include "SdkSpk.h"

#define DEF_TAG             "Spk"

int g_ChipSdk_ao_devID = 0;
int g_ChipSdk_ao_chnID = 0;

int Ext_SdkSpk_Init(void)
{
    int ret;
    /* Step 1: set public attribute of AO device. */
    IMPAudioIOAttr attr;
    attr.samplerate = AUDIO_SAMPLE_RATE_8000;
    attr.bitwidth = AUDIO_BIT_WIDTH_16;
    attr.soundmode = AUDIO_SOUND_MODE_MONO;
    attr.frmNum = 2;
    attr.numPerFrm = 320;
    attr.chnCnt = 1;

    ret = IMP_AO_SetPubAttr(g_ChipSdk_ao_devID, &attr);
    if (ret != 0)
    {
        IMP_LOG_ERR(DEF_TAG, "set ao %d attr err: %d\n", g_ChipSdk_ao_devID, ret);
        return -1;
    }

    memset(&attr, 0x0, sizeof(attr));
    ret = IMP_AO_GetPubAttr(g_ChipSdk_ao_devID, &attr);
    if (ret != 0)
    {
        IMP_LOG_ERR(DEF_TAG, "get ao %d attr err: %d\n", g_ChipSdk_ao_devID, ret);
        return -1;
    }

    PRT_INFO(DEF_TAG, "Audio Out GetPubAttr samplerate:%d\n", attr.samplerate);
    PRT_INFO(DEF_TAG, "Audio Out GetPubAttr   bitwidth:%d\n", attr.bitwidth);
    PRT_INFO(DEF_TAG, "Audio Out GetPubAttr  soundmode:%d\n", attr.soundmode);
    PRT_INFO(DEF_TAG, "Audio Out GetPubAttr     frmNum:%d\n", attr.frmNum);
    PRT_INFO(DEF_TAG, "Audio Out GetPubAttr  numPerFrm:%d\n", attr.numPerFrm);
    PRT_INFO(DEF_TAG, "Audio Out GetPubAttr     chnCnt:%d\n", attr.chnCnt);

    /* Step 2: enable AO device. */
    ret = IMP_AO_Enable(g_ChipSdk_ao_devID);
    if (ret != 0)
    {
        IMP_LOG_ERR(DEF_TAG, "enable ao %d err\n", g_ChipSdk_ao_devID);
        return -1;
    }

    /* Step 3: enable AI channel. */
    ret = IMP_AO_EnableChn(g_ChipSdk_ao_devID, g_ChipSdk_ao_chnID);
    if (ret != 0)
    {
        IMP_LOG_ERR(DEF_TAG, "Audio play enable channel failed\n");
        return -1;
    }

    /* Step 4: Set audio channel volume. */
    int chnVol = 80;
    ret = IMP_AO_SetVol(g_ChipSdk_ao_devID, g_ChipSdk_ao_chnID, chnVol);
    if (ret != 0)
    {
        IMP_LOG_ERR(DEF_TAG, "Audio Play set volume failed\n");
        return -1;
    }

    ret = IMP_AO_GetVol(g_ChipSdk_ao_devID, g_ChipSdk_ao_chnID, &chnVol);
    if (ret != 0)
    {
        IMP_LOG_ERR(DEF_TAG, "Audio Play get volume failed\n");
        return -1;
    }
    PRT_INFO(DEF_TAG, "Audio Out GetVol    vol:%d\n", chnVol);

    int aogain = 23;
    ret = IMP_AO_SetGain(g_ChipSdk_ao_devID, g_ChipSdk_ao_chnID, aogain);
    if (ret != 0)
    {
        IMP_LOG_ERR(DEF_TAG, "Audio Record Set Gain failed\n");
        return -1;
    }

    ret = IMP_AO_GetGain(g_ChipSdk_ao_devID, g_ChipSdk_ao_chnID, &aogain);
    if (ret != 0)
    {
        IMP_LOG_ERR(DEF_TAG, "Audio Record Get Gain failed\n");
        return -1;
    }

    PRT_INFO(DEF_TAG, "Audio Out GetGain    gain : %d\n", aogain);
    /* Step 5: enable AO algorithm. */
    ret = IMP_AO_EnableAlgo(g_ChipSdk_ao_devID, g_ChipSdk_ao_chnID);
    if (ret != 0)
    {
        IMP_LOG_ERR(DEF_TAG, "IMP_AO_EnableAlgo failed\n");
        return -1;
    }

    return 0;
}

int Ext_SdkSpk_Exit(void)
{
    int ret;

    IMP_AO_ClearChnBuf(g_ChipSdk_ao_devID, g_ChipSdk_ao_chnID);

    ret = IMP_AO_FlushChnBuf(g_ChipSdk_ao_devID, g_ChipSdk_ao_chnID);
    if (ret != 0)
    {
        IMP_LOG_ERR(DEF_TAG, "IMP_AO_FlushChnBuf error\n");
        return -1;
    }

    /* Step 7: disable AO algorithm. */
    ret = IMP_AO_DisableAlgo(g_ChipSdk_ao_devID, g_ChipSdk_ao_chnID);
    if (ret != 0)
    {
        IMP_LOG_ERR(DEF_TAG, "IMP_AO_DisableAlgo error\n");
        return -1;
    }

    /* Step 8: disable the audio channel. */
    ret = IMP_AO_DisableChn(g_ChipSdk_ao_devID, g_ChipSdk_ao_chnID);
    if (ret != 0)
    {
        IMP_LOG_ERR(DEF_TAG, "Audio channel disable error\n");
        return -1;
    }

    /* Step 9: disable the audio devices. */
    ret = IMP_AO_Disable(g_ChipSdk_ao_devID);
    if (ret != 0)
    {
        IMP_LOG_ERR(DEF_TAG, "Audio device disable error\n");
        return -1;
    }

    return 0;
}

void Ext_SdkMediaCom_SetSpkVolume(Com_s32 volume)
{
}

void Ext_SdkMediaCom_SetSpkGain(Com_s32 gain)
{
}

Com_void Ext_SdkSpk_Play(Com_pu8 buf,Com_u32 size)
{
    int ret;

    Ext_InSdkSpkCtrl_Using();

    IMPAudioFrame frm;
    frm.virAddr = (uint32_t *)buf;
    frm.len = size;
    ret = IMP_AO_SendFrame(g_ChipSdk_ao_devID, g_ChipSdk_ao_chnID, &frm, BLOCK);
    if (ret != 0)
    {
        PRT_ERR(DEF_TAG, "send Frame Data error!\n");
        return;
    }
}

Com_s32 Ext_SdkSpk_Send_Buff(Com_u8 *buf,Com_u32 size)
{
    Com_s32 ret = -1;

    Ext_InSdkSpkCtrl_Using();

    IMPAudioFrame frm;
    frm.virAddr = (uint32_t *)buf;
    frm.len = DEF_SDK_SPK_BUF_SIZE;
    ret = IMP_AO_SendFrame(g_ChipSdk_ao_devID, g_ChipSdk_ao_chnID, &frm, BLOCK);
    if (ret != 0)
    {
        PRT_ERR(DEF_TAG, "send Frame Data error!\n");
        return -1;
    }

    return 0;
}

Com_void Ext_SdkSpk_Send_End_Buff(Com_void)
{
    int i;
    int ret;
    char buf[DEF_SDK_SPK_BUF_SIZE];

    for(i = 0;i < 3;i++)
    {
        memset(buf,0,DEF_SDK_SPK_BUF_SIZE);

        IMPAudioFrame frm;
        frm.virAddr = (uint32_t *)buf;
        frm.len = DEF_SDK_SPK_BUF_SIZE;
        ret = IMP_AO_SendFrame(g_ChipSdk_ao_devID, g_ChipSdk_ao_chnID, &frm, BLOCK);
        if (ret != 0)
        {
            PRT_ERR(DEF_TAG, "send Frame Data error!\n");
            return;
        }
    }
}
